{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div align=\"right\">Peter Norvig 2014</div>\n",
    "\n",
    "# Sol Golomb’s Rectangle Puzzle"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This problem by Solomon Golomb was presented by Gary Antonik in his 14/4/14 New York Times [Numberplay column](http://wordplay.blogs.nytimes.com/2014/04/14/rectangle):\n",
    "\n",
    ">Say you’re given the following challenge: create a set of five rectangles that have sides of length 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10 units. You can combine sides in a variety of ways: for example, you could create a set of rectangles with dimensions 1 x 3, 2 x 4, 5 x 7, 6 x 8 and 9 x 10.\n",
    ">\n",
    ">1. How many different sets of five rectangles are possible?\n",
    ">\n",
    ">2. What are the maximum and minimum values for the total areas of the five rectangles?\n",
    ">\n",
    ">3. What other values for the total areas of the five rectangles are possible?\n",
    ">\n",
    ">4. Which sets of rectangles may be assembled to form a square?\n",
    "\n",
    "To me, these are interesting questions because, first, I have a (slight) personal connection to Solomon Golomb (my former colleague at USC) and to Nelson Blachman (the father of my colleague Nancy Blachman), who presented the problem to  Antonik, and second, I find it interesting that the problems span the range from mathematical to computational. Let's answer them."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. How many different sets of five rectangles are possible?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is a basic [combinatorics](http://en.wikipedia.org/wiki/Combinatorics) or counting problem. I will present *three* methods to count the sets. If all goes well they will give the same answer. The example set of rectangles given in the problem was\n",
    "\n",
    "> {1 &times; 3, 2 &times; 4, 5 &times; 7, 6 &times; 8, 9 &times; 10}\n",
    "    \n",
    "and in general it would be\n",
    "\n",
    "> {A &times; B, C &times; D, E &times; F, G &times; H, I &times; J}\n",
    "\n",
    "The question is: how many distinct ways can we assign the integers 1 through 10 to the variables A through J?\n",
    "    \n",
    "**Method 1: Count all permutations and divide by repetitions:** There are 10 variables to be filled, so there are 10! = 3,628,800 permutations.  But if we fill the first two variables with 1 &times; 3, that is the same rectangle as 3 &times; 1. So divide 10! by 2<sup>5</sup> to account for the fact that each of 5 rectangles can appear 2 ways.  Similarly, if we fill A and B with 1 &times; 3, that yields the same set as if we filled C and D with 1 &times; 3.  So divide again by 5! (the number of permutations of 5 things) to account for this.\n",
    "That gives us:\n",
    "\n",
    "> <a href=\"https://www.google.com/search?ie=UTF-8#q=10!+%2F+2%5E5+/+5!\">10! / 2<sup>5</sup> / 5!</a> = 945.  \n",
    "\n",
    "(It is always a relief when this \"count and divide\" method comes out to a whole number.)\n",
    "\n",
    "**Method 2: Count without repetitions**: in each rectangle of the example set the smaller component is listed first, and in each set, the rectangles with smaller first components are listed first.  An alternate to \"count and divide\" is to count directly how many sets there are that respect this ordering.  We'll work from left to right.  How many choices are there for variable A?  Only one: A must always be 1, because we agreed that the smallest number comes first. Then, given A, there are 9 remaining choices for B.  For C, given A and B, there is again only one choice: C must be the smallest of the remaining 8 numbers (it will be 3 if the first rectangle was 1 &times; 2; otherwise it will be 2, but either way there is only one choice).  That leaves 7 choices for D, 5 for F, 3 for H and 1 for J. So:\n",
    "\n",
    "> 9 &times; 7 &times; 5 &times; 3 &times; 1 = 945.\n",
    "\n",
    "(It is always a relief when two methods give the same answer.)\n",
    "          \n",
    "**Method 3: Write a program to enumerate the sets:** We'll represent the 1 &times; 3 rectangle as the tuple `(1, 3)` and the example set of rectangles as the set\n",
    "\n",
    "    {(1, 3), (2, 4), (5, 7), (6, 8), (9, 10)}\n",
    "\n",
    "We'll write a program to generate all possible sets of rectangles, following method 2, and then just count how many there are. To implement method 2, the minimum side will always be the first element, A, in an (A, B) pair. We iterate through all possible values for B, and then join that pair with all possible rectangles made from the remaining sides. We also have to handle the case when there are no sides; then there is one possible set of rectangles: the empty set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rectangle_sets(sides):\n",
    "    \"Given a set of sides, list all distinct sets of rectangles that can be made.\"\n",
    "    if not sides:\n",
    "        return [ set() ]\n",
    "    else:\n",
    "        A = min(sides)\n",
    "        return [ {(A, B)} | other_rects\n",
    "                for B in sides if B is not A\n",
    "                for other_rects in rectangle_sets(sides - {A, B}) ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{(1, 2), (3, 4)}, {(1, 3), (2, 4)}, {(1, 4), (2, 3)}]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rectangle_sets({1, 2, 3, 4})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We see above that there are three possible rectangle sets for 4 sides (the 1 can pair with any other side, and then a second rectangle can only be made one way from the remaining 2 sides). Below we count how many can be made with 10 sides:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "945"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sides = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}\n",
    "\n",
    "all_sets = rectangle_sets(sides)\n",
    "\n",
    "len(all_sets)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(It is a relief that once again we get the same answer, 945.)  \n",
    "\n",
    "I don't want to print all 945 sets, but let's peek at every 100th one:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)},\n",
       " {(1, 2), (3, 10), (4, 8), (5, 6), (7, 9)},\n",
       " {(1, 3), (2, 10), (4, 6), (5, 7), (8, 9)},\n",
       " {(1, 4), (2, 10), (3, 5), (6, 8), (7, 9)},\n",
       " {(1, 5), (2, 9), (3, 8), (4, 6), (7, 10)},\n",
       " {(1, 6), (2, 9), (3, 5), (4, 7), (8, 10)},\n",
       " {(1, 7), (2, 9), (3, 4), (5, 8), (6, 10)},\n",
       " {(1, 8), (2, 7), (3, 9), (4, 5), (6, 10)},\n",
       " {(1, 9), (2, 7), (3, 5), (4, 6), (8, 10)},\n",
       " {(1, 10), (2, 7), (3, 4), (5, 8), (6, 9)}]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_sets[::100]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. What are the maximum and minimum values for the total areas of the five rectangles?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "I think I know this one, but I'm not completely sure.  I know that a rectangle with a fixed perimeter has maximum area when it is a square. My guess is that the maximum total area occurs when each rectangle is *almost* square: a 9 &times; 10 rectangle; 7 &times; 8; and so on.  So that would give us a maximum area of:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "190"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1 * 2) + (3 * 4) + (5 * 6) + (7 * 8) + (9  * 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And the minimum area should be when the rectangles deviate the most from squares: a 1 &times; 10, and so on:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "110"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1 * 10) + (2 * 9) + (3 * 8) + (4 * 7) + (5 * 6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since I am not sure, I will double check by finding the rectangle sets with the min and max total areas:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def total_area(rectangles): return sum(w * h for (w, h) in rectangles)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max(all_sets, key=total_area)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(1, 10), (2, 9), (3, 8), (4, 7), (5, 6)}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "min(all_sets, key=total_area)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This verifies my intuitions. But I still don't completely understand the situation.  Suppose there are *N* sides that are not  consecutive integers.  Will the maximum total area always be formed by combining the two biggest sides, and so on?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. What other values for the total areas of the five rectangles are possible?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I have no idea how to figure this out mathematically from first principles, but it is easy to compute with the code we already have:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 181, 182, 183, 184, 186, 187, 190]\n"
     ]
    }
   ],
   "source": [
    "areas = {total_area(s) for s in all_sets}\n",
    "print(sorted(areas))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Is there a more succint way to describe these values? Let's see ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{176, 185, 188, 189}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set(range(110, 191)) - areas"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The answer is: \"All the integers between 110 and 190 inclusive, except 176, 185, 188, and 189.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Which sets of rectangles may be assembled to form a square?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The only way I can think about this is to write a program; I don't see any way to work it out by hand.  I do know that the total area will have to be a perfect square, and since the total area is between 110 and 191, that means either 121, 144, or 169:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(121, {(1, 6), (2, 9), (3, 10), (4, 8), (5, 7)}),\n",
       " (121, {(1, 6), (2, 10), (3, 8), (4, 9), (5, 7)}),\n",
       " (121, {(1, 6), (2, 10), (3, 9), (4, 7), (5, 8)}),\n",
       " (121, {(1, 7), (2, 10), (3, 6), (4, 9), (5, 8)}),\n",
       " (121, {(1, 8), (2, 6), (3, 10), (4, 9), (5, 7)}),\n",
       " (121, {(1, 8), (2, 7), (3, 10), (4, 6), (5, 9)}),\n",
       " (121, {(1, 8), (2, 9), (3, 7), (4, 6), (5, 10)}),\n",
       " (121, {(1, 8), (2, 10), (3, 5), (4, 9), (6, 7)}),\n",
       " (121, {(1, 9), (2, 7), (3, 6), (4, 10), (5, 8)}),\n",
       " (121, {(1, 9), (2, 7), (3, 8), (4, 6), (5, 10)}),\n",
       " (121, {(1, 9), (2, 7), (3, 10), (4, 5), (6, 8)}),\n",
       " (121, {(1, 9), (2, 8), (3, 6), (4, 7), (5, 10)}),\n",
       " (121, {(1, 10), (2, 5), (3, 9), (4, 8), (6, 7)}),\n",
       " (121, {(1, 10), (2, 8), (3, 7), (4, 5), (6, 9)}),\n",
       " (144, {(1, 3), (2, 9), (4, 10), (5, 7), (6, 8)}),\n",
       " (144, {(1, 3), (2, 10), (4, 7), (5, 9), (6, 8)}),\n",
       " (144, {(1, 3), (2, 10), (4, 8), (5, 7), (6, 9)}),\n",
       " (144, {(1, 5), (2, 6), (3, 8), (4, 10), (7, 9)}),\n",
       " (144, {(1, 7), (2, 4), (3, 8), (5, 9), (6, 10)}),\n",
       " (144, {(1, 7), (2, 9), (3, 5), (4, 6), (8, 10)}),\n",
       " (144, {(1, 9), (2, 5), (3, 7), (4, 6), (8, 10)}),\n",
       " (144, {(1, 9), (2, 6), (3, 5), (4, 7), (8, 10)}),\n",
       " (144, {(1, 10), (2, 4), (3, 5), (6, 8), (7, 9)}),\n",
       " (169, {(1, 2), (3, 5), (4, 9), (6, 10), (7, 8)}),\n",
       " (169, {(1, 2), (3, 7), (4, 6), (5, 10), (8, 9)}),\n",
       " (169, {(1, 2), (3, 7), (4, 9), (5, 6), (8, 10)}),\n",
       " (169, {(1, 2), (3, 8), (4, 5), (6, 10), (7, 9)}),\n",
       " (169, {(1, 3), (2, 5), (4, 8), (6, 9), (7, 10)}),\n",
       " (169, {(1, 3), (2, 7), (4, 5), (6, 10), (8, 9)}),\n",
       " (169, {(1, 3), (2, 7), (4, 8), (5, 6), (9, 10)}),\n",
       " (169, {(1, 4), (2, 5), (3, 7), (6, 9), (8, 10)}),\n",
       " (169, {(1, 5), (2, 3), (4, 9), (6, 7), (8, 10)}),\n",
       " (169, {(1, 5), (2, 4), (3, 8), (6, 7), (9, 10)}),\n",
       " (169, {(1, 5), (2, 7), (3, 4), (6, 8), (9, 10)}),\n",
       " (169, {(1, 6), (2, 3), (4, 8), (5, 7), (9, 10)})]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "perfect_squares = {i ** 2 for i in range(100)}\n",
    "\n",
    "sorted((total_area(s), s) \n",
    "       for s in all_sets if total_area(s) in perfect_squares)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "35"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So 35 out of 945 rectangle sets *might* be assembled into a square; we don't know yet if they *can* be. \n",
    "\n",
    "# Packing Rectangles into a Square\n",
    "\n",
    "I would like to see *how* the rectangles are packed into the square, not just *which* sets can be packed, so I'll need a data structure to hold information on which rectangle goes where, and a way to visually display the results.\n",
    "I'll represent a *Grid* as a two-dimensional array: a list of rows, each of which is a list of cells, with the idea that each cell will be covered by a rectangle by assigning `grid[y][x] = rect` (and the special rectangle `empty` will be used to mark a cell as uncovered). I only need to deal with square grids:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "empty = (0, 0)\n",
    "\n",
    "def Square(n):\n",
    "    \"Create an [n x n] grid of empty cells.\"\n",
    "    return [[empty for col in range(n)] \n",
    "            for row in range(n)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],\n",
       " [(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],\n",
       " [(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],\n",
       " [(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],\n",
       " [(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)]]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Square(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's make a function to place a single rectangle onto a grid:  `place_rectangle_at((w, h), grid, (x0, y0))` places a rectangle of width *w* and height *h* onto a grid at position (x<sub>0</sub>, y<sub>0</sub>).  If the rectangle overlaps a non-empty cell, or goes off the grid, we return `None` to indicate that this is not a legal placement. If the rectangle does fit, we return a new grid with the new rectangle placed on it (we do not modify the original grid):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def place_rectangle_at(rect, grid, pos):\n",
    "    \"\"\"Place the rectangle of size (w, h) onto grid at position (x0, y0).\n",
    "    Return a new grid, or None if the rectangle cannot be placed.\"\"\"\n",
    "    (w, h)   = rect\n",
    "    (x0, y0) = pos\n",
    "    newgrid  = [row.copy() for row in grid] \n",
    "    for x in range(x0, x0+w):\n",
    "        for y in range(y0, y0+h):\n",
    "            if y >= len(grid) or x >= len(grid[y]) or newgrid[y][x] is not empty:\n",
    "                return None   \n",
    "            newgrid[y][x] = rect\n",
    "    return newgrid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],\n",
       " [(0, 0), (0, 0), (3, 4), (3, 4), (3, 4)],\n",
       " [(0, 0), (0, 0), (3, 4), (3, 4), (3, 4)],\n",
       " [(0, 0), (0, 0), (3, 4), (3, 4), (3, 4)],\n",
       " [(0, 0), (0, 0), (3, 4), (3, 4), (3, 4)]]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Place a 3 x 4 rectangle on a 5 x 5 square grid, 2 cells over and 1 down:\n",
    "place_rectangle_at((3, 4), Square(5), (2, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[(2, 5), (2, 5), (0, 0), (0, 0), (0, 0)],\n",
       " [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)],\n",
       " [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)],\n",
       " [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)],\n",
       " [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)]]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Place two rectangles on a grid\n",
    "grid0 = Square(5)\n",
    "grid1 = place_rectangle_at((3, 4), grid0, (2, 1))\n",
    "grid2 = place_rectangle_at((2, 5), grid1, (0, 0))\n",
    "grid2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[(2, 5), (2, 5), (3, 1), (3, 1), (3, 1)],\n",
       " [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)],\n",
       " [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)],\n",
       " [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)],\n",
       " [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)]]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Place a third rectangle that just barely fits\n",
    "place_rectangle_at((3, 1), grid2, (2, 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "# Alternatively, try to place a third rectangle that does not fit\n",
    "print(place_rectangle_at((4, 1), grid2, (2, 0)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we need a strategy for packing a set of rectangles onto a grid.  I know that many variants of [bin packing problems](http://en.wikipedia.org/wiki/Bin_packing_problem) are NP-hard, but we only have 5 rectangles, so it should be easy: just exhaustively try each rectangle in each possible position, and in both possible orientations (horizontal and vertical). But placing rectangles is commutative, so we can do this two ways:\n",
    "\n",
    "> Way 1: Considering the *rectangles* in a fixed order, try every possible *position* for each *rectangle*.\n",
    "\n",
    "or\n",
    "\n",
    "> Way 2: Considering the *positions* in a fixed order, try every possible *rectangle* for each *position*.\n",
    "\n",
    "In Way 1, we could pre-sort the rectangles (say, biggest first).  Then we try to put the biggest rectangle in all possible positions on the grid, and for each position that fits, try putting the second biggest rectangle in all remaining positions, and so on.  As a rough estimate, assume there are on average about 10 ways to place a rectangle.  Then this way will look at about 10<sup>5</sup> = 100,000 combinations.\n",
    "\n",
    "In Way 2, we consider the positions in some fixed order; say top-to-bottom, left-to right.  Take the first empty position (say, the upper left corner).  Try putting each of the rectangles there, and for each one that fits, try all possible rectangles in the next empty position, and so on. There are only 5! permutations of rectangles, and each rectangle can go either horizontaly or vertically, so we would have to consider 5! &times; 2<sup>5</sup> = 3840 combinations.  Since 3840 &lt; 100,000, I'll go with Way 2.  Here is a more precise description:\n",
    "\n",
    "> Way 2: To `pack` a set of rectangles onto a grid, find the first empty cell on the grid.  Try in turn all possible placements of any rectangle (in either orientation) at that position. For each one that fits, try to `pack` the remaining rectangles, and return the resulting grid if one of these packings succeeds.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pack(rectangles, grid):\n",
    "    \"\"\"Find a way to pack all rectangles onto grid and return the packed grid,\n",
    "    or return None if not possible.\"\"\"\n",
    "    if not rectangles:\n",
    "        return grid \n",
    "    pos = first_empty_cell(grid)\n",
    "    if grid and pos:\n",
    "        for (rectangles2, grid2) in rectangle_placements(rectangles, grid, pos):\n",
    "            solution = pack(rectangles2, grid2)\n",
    "            if solution:\n",
    "                return solution\n",
    "\n",
    "def rectangle_placements(rectangles, grid, pos):\n",
    "    \"Yield all (rect, grid) pairs that result from placing a rectangle at pos on grid.\"\n",
    "    for (w, h) in rectangles:\n",
    "        for rect in [(w, h), (h, w)]:\n",
    "            grid2 = place_rectangle_at(rect, grid, pos)\n",
    "            if grid2: \n",
    "                yield rectangles - {(w, h)}, grid2       \n",
    "                \n",
    "def first_empty_cell(grid):\n",
    "    \"The uppermost, leftmost empty cell.\"\n",
    "    for (y, row) in enumerate(grid):\n",
    "        for (x, cell) in enumerate(row):\n",
    "            if cell is empty:\n",
    "                return (x, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's try a simple example that I know will fit:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[(2, 5), (2, 5), (3, 1), (3, 1), (3, 1)],\n",
       " [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)],\n",
       " [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)],\n",
       " [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)],\n",
       " [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)]]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack({(1, 3), (2, 5), (3, 4)}, Square(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It works! But it is not pretty.\n",
    "\n",
    "# Colored Rectangles"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It would be nicer to have a graphical display of colored rectangles. I will define the function `show` which displays a grid as colored rectangles, by calling upon `html_table`, which formats any grid into HTML text. (*Note:* I used the deprecated `bgcolor` attribute of HTML, rather than CSS styles, because Github is conservative in the javascript and even CSS that it allows, and wouldn't display colored styles.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import HTML, display, clear_output\n",
    "\n",
    "def show(grid):\n",
    "    \"Display a colored HTML representation of this grid, unless it is None.\"\n",
    "    if grid:\n",
    "        display(html_table(grid, colored_cell))\n",
    "    \n",
    "def html_table(grid, cell_function='<td>{}'.format):\n",
    "    \"\"\"Return an HTML <table>, where each cell's contents comes from calling \n",
    "    cell_function(grid[y][x])\"\"\"\n",
    "    return HTML('<table>{}</table>'\n",
    "                .format(cat('\\n<tr>' + cat(map(cell_function, row)) \n",
    "                        for row in grid)))\n",
    "\n",
    "def colored_cell(rect): \n",
    "    x, y = sorted(rect)\n",
    "    return '<td bgcolor=\"{}\">{}{}'.format(colors[x], x%10, y%10)\n",
    "\n",
    "colors = 'lightgrey yellow plum lime cyan coral red olive slateblue wheat'.split()\n",
    "\n",
    "cat = ''.join"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<tr><td bgcolor=\"plum\">25<td bgcolor=\"plum\">25<td bgcolor=\"yellow\">13<td bgcolor=\"yellow\">13<td bgcolor=\"yellow\">13\n",
       "<tr><td bgcolor=\"plum\">25<td bgcolor=\"plum\">25<td bgcolor=\"lime\">34<td bgcolor=\"lime\">34<td bgcolor=\"lime\">34\n",
       "<tr><td bgcolor=\"plum\">25<td bgcolor=\"plum\">25<td bgcolor=\"lime\">34<td bgcolor=\"lime\">34<td bgcolor=\"lime\">34\n",
       "<tr><td bgcolor=\"plum\">25<td bgcolor=\"plum\">25<td bgcolor=\"lime\">34<td bgcolor=\"lime\">34<td bgcolor=\"lime\">34\n",
       "<tr><td bgcolor=\"plum\">25<td bgcolor=\"plum\">25<td bgcolor=\"lime\">34<td bgcolor=\"lime\">34<td bgcolor=\"lime\">34</table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# For example:\n",
    "show(pack({(1, 3), (2, 5), (3, 4)}, Square(5)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's much nicer to look at! Now we can show all the rectangles that are packable into a square:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<tr><td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50\n",
       "<tr><td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50\n",
       "<tr><td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50\n",
       "<tr><td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50\n",
       "<tr><td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50\n",
       "<tr><td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50\n",
       "<tr><td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50\n",
       "<tr><td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50\n",
       "<tr><td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"slateblue\">89<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50\n",
       "<tr><td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"yellow\">12<td bgcolor=\"yellow\">12<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50\n",
       "<tr><td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"lime\">37<td bgcolor=\"lime\">37<td bgcolor=\"lime\">37<td bgcolor=\"lime\">37<td bgcolor=\"lime\">37<td bgcolor=\"lime\">37<td bgcolor=\"lime\">37\n",
       "<tr><td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"lime\">37<td bgcolor=\"lime\">37<td bgcolor=\"lime\">37<td bgcolor=\"lime\">37<td bgcolor=\"lime\">37<td bgcolor=\"lime\">37<td bgcolor=\"lime\">37\n",
       "<tr><td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"cyan\">46<td bgcolor=\"lime\">37<td bgcolor=\"lime\">37<td bgcolor=\"lime\">37<td bgcolor=\"lime\">37<td bgcolor=\"lime\">37<td bgcolor=\"lime\">37<td bgcolor=\"lime\">37</table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<tr><td bgcolor=\"lime\">38<td bgcolor=\"lime\">38<td bgcolor=\"lime\">38<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60\n",
       "<tr><td bgcolor=\"lime\">38<td bgcolor=\"lime\">38<td bgcolor=\"lime\">38<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60\n",
       "<tr><td bgcolor=\"lime\">38<td bgcolor=\"lime\">38<td bgcolor=\"lime\">38<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60\n",
       "<tr><td bgcolor=\"lime\">38<td bgcolor=\"lime\">38<td bgcolor=\"lime\">38<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60\n",
       "<tr><td bgcolor=\"lime\">38<td bgcolor=\"lime\">38<td bgcolor=\"lime\">38<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60\n",
       "<tr><td bgcolor=\"lime\">38<td bgcolor=\"lime\">38<td bgcolor=\"lime\">38<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60<td bgcolor=\"red\">60\n",
       "<tr><td bgcolor=\"lime\">38<td bgcolor=\"lime\">38<td bgcolor=\"lime\">38<td bgcolor=\"yellow\">12<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79\n",
       "<tr><td bgcolor=\"lime\">38<td bgcolor=\"lime\">38<td bgcolor=\"lime\">38<td bgcolor=\"yellow\">12<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79\n",
       "<tr><td bgcolor=\"cyan\">45<td bgcolor=\"cyan\">45<td bgcolor=\"cyan\">45<td bgcolor=\"cyan\">45<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79\n",
       "<tr><td bgcolor=\"cyan\">45<td bgcolor=\"cyan\">45<td bgcolor=\"cyan\">45<td bgcolor=\"cyan\">45<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79\n",
       "<tr><td bgcolor=\"cyan\">45<td bgcolor=\"cyan\">45<td bgcolor=\"cyan\">45<td bgcolor=\"cyan\">45<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79\n",
       "<tr><td bgcolor=\"cyan\">45<td bgcolor=\"cyan\">45<td bgcolor=\"cyan\">45<td bgcolor=\"cyan\">45<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79\n",
       "<tr><td bgcolor=\"cyan\">45<td bgcolor=\"cyan\">45<td bgcolor=\"cyan\">45<td bgcolor=\"cyan\">45<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79<td bgcolor=\"olive\">79</table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<tr><td bgcolor=\"lime\">39<td bgcolor=\"lime\">39<td bgcolor=\"lime\">39<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58\n",
       "<tr><td bgcolor=\"lime\">39<td bgcolor=\"lime\">39<td bgcolor=\"lime\">39<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58\n",
       "<tr><td bgcolor=\"lime\">39<td bgcolor=\"lime\">39<td bgcolor=\"lime\">39<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58\n",
       "<tr><td bgcolor=\"lime\">39<td bgcolor=\"lime\">39<td bgcolor=\"lime\">39<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58\n",
       "<tr><td bgcolor=\"lime\">39<td bgcolor=\"lime\">39<td bgcolor=\"lime\">39<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58<td bgcolor=\"coral\">58\n",
       "<tr><td bgcolor=\"lime\">39<td bgcolor=\"lime\">39<td bgcolor=\"lime\">39<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"yellow\">16\n",
       "<tr><td bgcolor=\"lime\">39<td bgcolor=\"lime\">39<td bgcolor=\"lime\">39<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"yellow\">16\n",
       "<tr><td bgcolor=\"lime\">39<td bgcolor=\"lime\">39<td bgcolor=\"lime\">39<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"yellow\">16\n",
       "<tr><td bgcolor=\"lime\">39<td bgcolor=\"lime\">39<td bgcolor=\"lime\">39<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"yellow\">16\n",
       "<tr><td bgcolor=\"plum\">20<td bgcolor=\"plum\">20<td bgcolor=\"plum\">20<td bgcolor=\"plum\">20<td bgcolor=\"plum\">20<td bgcolor=\"plum\">20<td bgcolor=\"plum\">20<td bgcolor=\"plum\">20<td bgcolor=\"plum\">20<td bgcolor=\"plum\">20<td bgcolor=\"yellow\">16\n",
       "<tr><td bgcolor=\"plum\">20<td bgcolor=\"plum\">20<td bgcolor=\"plum\">20<td bgcolor=\"plum\">20<td bgcolor=\"plum\">20<td bgcolor=\"plum\">20<td bgcolor=\"plum\">20<td bgcolor=\"plum\">20<td bgcolor=\"plum\">20<td bgcolor=\"plum\">20<td bgcolor=\"yellow\">16</table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"plum\">28<td bgcolor=\"plum\">28\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"plum\">28<td bgcolor=\"plum\">28\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"plum\">28<td bgcolor=\"plum\">28\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"plum\">28<td bgcolor=\"plum\">28\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"plum\">28<td bgcolor=\"plum\">28\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"plum\">28<td bgcolor=\"plum\">28\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"plum\">28<td bgcolor=\"plum\">28\n",
       "<tr><td bgcolor=\"yellow\">19<td bgcolor=\"yellow\">19<td bgcolor=\"yellow\">19<td bgcolor=\"yellow\">19<td bgcolor=\"yellow\">19<td bgcolor=\"yellow\">19<td bgcolor=\"yellow\">19<td bgcolor=\"yellow\">19<td bgcolor=\"yellow\">19<td bgcolor=\"plum\">28<td bgcolor=\"plum\">28</table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def pack_square(rectangles):\n",
    "    \"Pack rectangles into a square of appropriate size, if possible.\"\n",
    "    A = total_area(rectangles)\n",
    "    if A in perfect_squares:\n",
    "        return pack(rectangles, Square(int(A ** 0.5)))\n",
    "\n",
    "for s in all_sets:\n",
    "    show(pack_square(s))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So there are 4 sets of rectangles that work."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Animated Colored Rectangles"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is gratifying to see the final results, and have the computation be so fast, but I'd like to get a better understanding of the algorithm.  I can visualize the process by *animating* the search that `pack` does: on every recursive call to `pack`, I can show the grid, and then pause briefly. I do that by redefining `pack`; the only change is the line starting `if pause:`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "def pack(rectangles, grid, pause=None):\n",
    "    \"\"\"Find a way to pack all rectangles onto grid and return the packed grid,\n",
    "    or return None if not possible.\"\"\"\n",
    "    if pause: (clear_output(), show(grid), time.sleep(pause))\n",
    "    if not rectangles:\n",
    "        return grid \n",
    "    pos = first_empty_cell(grid)\n",
    "    if grid and pos:\n",
    "        for (rectangles2, grid2) in rectangle_placements(rectangles, grid, pos):\n",
    "            solution = pack(rectangles2, grid2, pause)\n",
    "            if solution:\n",
    "                return solution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you are running this in a live IPython notebook (not in nbviewer), you can see for yourself by re-running this cell:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36<td bgcolor=\"lime\">36\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"plum\">28<td bgcolor=\"plum\">28\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"plum\">28<td bgcolor=\"plum\">28\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"plum\">28<td bgcolor=\"plum\">28\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"plum\">28<td bgcolor=\"plum\">28\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"plum\">28<td bgcolor=\"plum\">28\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"plum\">28<td bgcolor=\"plum\">28\n",
       "<tr><td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"coral\">50<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"cyan\">47<td bgcolor=\"plum\">28<td bgcolor=\"plum\">28\n",
       "<tr><td bgcolor=\"yellow\">19<td bgcolor=\"yellow\">19<td bgcolor=\"yellow\">19<td bgcolor=\"yellow\">19<td bgcolor=\"yellow\">19<td bgcolor=\"yellow\">19<td bgcolor=\"yellow\">19<td bgcolor=\"yellow\">19<td bgcolor=\"yellow\">19<td bgcolor=\"plum\">28<td bgcolor=\"plum\">28</table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pack({(5, 10), (3, 6), (4, 7), (2, 8), (1, 9)}, Square(11), 1);"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
